package logic

import (
	"context"
	"fmt"
	"weixin/moble/cache"
	"weixin/moble/mysql"

	"weixin/weixinrpc/internal/svc"
	"weixin/weixinrpc/weixinrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *weixinrpc.LoginRequest) (*weixinrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	grt := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile)
	if grt.Val() != in.SmsCode {
		return nil, fmt.Errorf("短信验证码错误,请重新输入")
	}
	mysql.DB.Debug().Find(&user)
	return &weixinrpc.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
